var t = require("../../../@babel/runtime/helpers/objectSpread2"), a = getApp();

Page({
    data: {
        activity_id: "",
        goods_id: "",
        options_id: "",
        activity_start: "",
        activity_end: "",
        goods_count: 1,
        backgroundRGB: [],
        actData: {
            deposit: 0
        },
        timeData: {},
        currentTime: parseInt(new Date().getTime() / 1e3),
        activity_order: [],
        ladderList: [],
        steps: 0,
        shareShow: !1,
        shareOptions: {},
        options: [ {
            name: "分享海报",
            icon: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/depositGroup/poster@2x.png",
            description: "生成海报"
        }, {
            name: "微信",
            icon: "wechat",
            openType: "share",
            description: "分享给好友"
        } ],
        cservice: {},
        posterShow: !1,
        goodsPopupShow: !1,
        showAgrPopup: !1,
        contractId: "",
        shareText: "",
        o2oStore: {}
    },
    onLoad: function(t) {
        if (t.scene) {
            var a = this.sceneDecode(t.scene).id;
            this.setData({
                activity_id: a
            });
        }
        t.id && this.setData({
            activity_id: t.id
        });
    },
    onShow: function() {
        this.getData();
    },
    sceneDecode: function(t) {
        var a = (t = decodeURIComponent(t)).split("&"), i = {};
        return a.forEach(function(t, a) {
            var e = t.split("=")[0], o = t.split("=")[1];
            i[e] = o;
        }), i;
    },
    getData: function() {
        var t = this, i = a.getNetAddresss("plugin.deposit-ladder.frontend.index.activity");
        a._getNetWork({
            url: i,
            data: {
                id: this.data.activity_id
            },
            success: function(a) {
                if (1 === a.data.result) {
                    var i = a.data, e = t.hexStringToRGB(i.data.activity.theme_color), o = t.replaceDetail(i.data.activity.activity_explain), s = i.data.activity.commission ? "每成功邀请到一位好友下定，可获得".concat(i.data.activity.commission, "元奖励") : "立即分享给好友";
                    try {
                        wx.setStorageSync("backgroundRGB", e);
                    } catch (t) {}
                    t.setData({
                        activity_id: i.data.activity.id,
                        goods_id: i.data.activity.goods_id,
                        goods_option_id: i.data.activity.goods_option_id,
                        activity_start: 1e3 * i.data.activity.activity_start - new Date(),
                        activity_end: 1e3 * i.data.activity.activity_end - new Date(),
                        actData: i.data.activity,
                        cservice: i.data.cservice,
                        backgroundRGB: e,
                        richNode: o,
                        shareOptions: i.data.activity.share,
                        shareText: s,
                        o2oStore: i.data.activity.has_one_polymorphism || {}
                    }), i.data.activity.activity_order.length > 4 ? t.setData({
                        recordList: i.data.activity.activity_order.slice(0, 4),
                        isMore: !0
                    }) : t.setData({
                        recordList: i.data.activity.activity_order,
                        isMore: !1
                    }), t.resetSteps(i.data.activity.ladder);
                } else wx.showModal({
                    content: a.data.msg,
                    showCancel: !1,
                    success: function() {
                        getCurrentPages().length > 1 ? wx.navigateBack({
                            delta: 1
                        }) : wx.redirectTo({
                            url: "/packageG/index/index"
                        });
                    }
                });
            },
            fail: function(t) {
                console.log("失败", t);
            }
        });
    },
    resetSteps: function(a) {
        var i = 0;
        a = a.reverse();
        var e = t({}, a[0]), o = this.data.actData;
        e.deal_price = o.bind_goods.price, a.unshift(e);
        for (var s = 0; s < a.length; s++) {
            if (0 === s && (1 === o.ladder_type && o.sale_count < a[s].sale_num || 0 === o.ladder_type && o.order_count < a[s].deal_num)) {
                i = 0;
                break;
            }
            (s > 0 && 1 === o.ladder_type && o.sale_count >= a[s].sale_num || 0 === o.ladder_type && o.order_count >= a[s].deal_num) && (i = s);
        }
        this.setData({
            steps: i,
            ladderList: a
        });
    },
    getGoodDetail: function() {
        var t = this;
        if (this.data.goodsInfo) this.setData({
            goodsPopupShow: !0
        }); else {
            var i = a.getNetAddresss("goods.goods.get-goods-page");
            a._postNetWork({
                url: i,
                data: {
                    id: Number(this.data.actData.bind_goods.id)
                },
                success: function(i) {
                    console.log(i), 0 != i.result ? t.setData({
                        goodsInfo: i.data.data.get_goods,
                        goodsPopupShow: !0
                    }) : a.tips(i.data.msg);
                }
            });
        }
    },
    changeGoodCount: function(t) {
        var a = 1 * t.currentTarget.dataset.num;
        this.handlerGoods(a);
    },
    goodsDetailCartAdd: function(t) {
        var a = 1 * t.detail;
        this.handlerGoods(a);
    },
    handlerGoods: function(t) {
        var i = this.data.goods_count + t;
        i < 1 ? a.tips("商品数量最低为1") : 0 !== this.data.actData.limit && i > this.data.actData.limit ? a.tips("单次限购".concat(this.data.actData.limit, "件")) : i + this.data.member_order_count > this.data.actData.limit_total ? a.tips("会员限购".concat(this.data.actData.limit_total, "件")) : this.setData({
            goods_count: i
        });
    },
    loadMore: function() {
        this.setData({
            recordList: this.data.actData.activity_order,
            isMore: !1
        });
    },
    openShareShow: function() {
        this.setData({
            shareShow: !0
        });
    },
    onCloseShare: function() {
        this.setData({
            shareShow: !1
        });
    },
    onSelectShare: function(t) {
        "复制链接" == t.detail.name ? wx.setClipboardData({
            data: t.detail.link,
            success: function() {
                a.tips("复制链接成功");
            },
            fail: function() {
                a.tips("复制链接失败");
            }
        }) : "分享海报" == t.detail.name && this.setData({
            posterShow: !0
        });
    },
    onChangeTime: function(t) {
        this.setData({
            timeData: t.detail
        });
    },
    buyNow: function() {
        this.data.goods_count < 1 ? a.tips("商品数量不能为零") : this.checkMax();
    },
    checkMax: function() {
        var t = this, i = a.getNetAddresss("plugin.deposit-ladder.frontend.store.index.check-max");
        a._getNetWork({
            url: i,
            data: {
                id: this.data.activity_id
            },
            success: function(i) {
                1 == i.data.result ? 0 === t.data.actData.agreement ? t.getContractStatue() : t.showAgreementPopup() : a.tips(i.data.msg);
            }
        });
    },
    getContractStatue: function() {
        var t = this, i = a.getNetAddresss("plugin.deposit-ladder.frontend.yun-sign.check-yun-sign");
        a._getNetWork({
            url: i,
            data: {
                goods_id: this.data.goods_id
            },
            success: function(a) {
                1 === a.data.result ? a.data.data.contract_id ? (t.setData({
                    contractId: a.data.data.contract_id
                }), t.showAgreementPopup()) : t.toOrder() : wx.showModal({
                    content: a.data.msg
                });
            }
        });
    },
    showAgreementPopup: function() {
        this.setData({
            showAgrPopup: !0
        });
    },
    agreement: function() {
        this.closeAgreementPopup(), this.setData({
            agreement: !0
        }), this.toOrder();
    },
    toOrder: function() {
        if (this.data.goods_id) {
            var t = "-2", a = this.data, i = "/packageD/buy/myOrder_v2/myOrder_v2?goodsId=" + a.goods_id + "&optionsId=0&total=" + a.goods_count;
            this.data.o2oStore && this.data.o2oStore.polymorphism_id && (i = i + "&store_id=" + this.data.o2oStore.polymorphism_id, 
            t = "o2oDeposit"), i = i + "&tag=" + t, wx.navigateTo({
                url: i
            });
        }
    },
    closeAgreementPopup: function() {
        this.setData({
            showAgrPopup: !1
        });
    },
    replaceDetail: function(t) {
        for (var a = ""; -1 != t.indexOf("<img"); ) a += t.substring("0", t.indexOf("<img") + 4), 
        -1 != (t = t.substring(t.indexOf("<img") + 4)).indexOf("style=") && t.indexOf("style=") < t.indexOf(">") ? (a += t.substring(0, t.indexOf('style="') + 7) + "max-width:100%;height:auto;margin:0 auto;", 
        t = t.substring(t.indexOf('style="') + 7)) : a += ' style="max-width:100%;height:auto;margin:0 auto;" ';
        return a += t;
    },
    hexStringToRGB: function(t) {
        t || (t = "#fec400");
        var a = t.toLowerCase();
        if (/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/.test(a)) {
            if (4 === a.length) {
                for (var i = "#", e = 1; e < 4; e += 1) i += a.slice(e, e + 1).concat(a.slice(e, e + 1));
                a = i;
            }
            for (var o = [], s = 1; s < 7; s += 2) o.push(parseInt("0x" + a.slice(s, s + 2)));
            return o;
        }
        return [];
    },
    gotoOrder: function() {
        wx.navigateTo({
            url: "/packageA/member/myOrder_v2/myOrder_v2"
        });
    },
    gotoSignPage: function() {
        wx.navigateTo({
            url: "/packageH/signIndexPlu/signPage?id=".concat(this.data.contractId)
        });
    },
    gotoRankList: function() {
        wx.navigateTo({
            url: "/packageH/deposit_group/rankList/rankList?id=".concat(this.data.activity_id)
        });
    },
    onShareAppMessage: function(t) {
        var a = wx.getStorageSync("yz_uid") || "";
        return this.data.shareOptions.share_title && this.data.shareOptions.share_img_src ? {
            path: "/packageH/deposit_group/activity_index/activity_index?id=".concat(this.data.activity_id, "&mid=").concat(a),
            title: this.data.shareOptions.share_title,
            imageUrl: this.data.shareOptions.share_img_src
        } : {
            path: "/packageH/deposit_group/activity_index/activity_index?id=".concat(this.data.activity_id, "&mid=").concat(a),
            title: "芸众商城",
            imageUrl: this.data.actData.goods_option && this.data.actData.goods_option.thumb || null
        };
    },
    onShareTimeline: function() {
        return this.data.shareOptions.share_title && this.data.shareOptions.share_img_src ? {
            query: "id=".concat(this.data.activity_id),
            title: this.data.shareOptions.share_title,
            imageUrl: this.data.shareOptions.share_img_src
        } : {
            path: "id=".concat(this.data.activity_id),
            title: "芸众商城"
        };
    }
});